package com.sdx.coop.exam.web;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.BaseEntity;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.HttpUtils;
import com.sdx.common.utils.Utils;
import com.sdx.coop.exam.service.ExamPapersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CustomerScController
 * @Description: 四川书商的用户管理
 * @author: tanbaocai
 * @date: 2019/3/21 16:24
 */
@RequestMapping("/coop/examPapers")
@Controller
public class CustomerScController {
	private static final String MODEL = "/coop/examPapers";


	@Resource
	private ExamPapersService examPapersService;

	@GetMapping("/customer.do")
	@Function("coop.examPapers.customer")
	@ResponseBody
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(MODEL+"/customer.html") ;
		return view;
	}

	@PostMapping("/customerCount.json")
	@Function("coop.examPapers.customer")
	@ResponseBody
	public JsonResult<Integer> customerCount(String regTimeQuery) throws ParseException {
		Date startDate = null;
		Date endDate = null;
		if (StringUtils.isNotEmpty(regTimeQuery)) {
			Date[] date = Utils.parseQueryTime(regTimeQuery);
			if (date.length==2) {
				startDate = date[0];
				endDate = date[1];
			}
		}
		int count = examPapersService.selectPhoneCount(startDate, endDate);
		return JsonResult.success(count);
	}

	@GetMapping("/phone.do")
	@Function("coop.examPapers.customer")
	public ResponseEntity<String> customerPhones(String regTimeQuery) throws ParseException {
		Date startDate = null;
		Date endDate = null;
		if (StringUtils.isNotEmpty(regTimeQuery)) {
			Date[] date = Utils.parseQueryTime(regTimeQuery);
			if (date.length==2) {
				startDate = date[0];
				endDate = date[1];
			}
		}
		List<BaseEntity> phoneList = examPapersService.selectPhones(startDate,endDate);
		StringBuffer strBuff = new StringBuffer();
		for (int i = 0; i < phoneList.size(); i++) {
			strBuff.append(phoneList.get(i).get("sourceId").toString()+"\r\n");
		}
		return HttpUtils.exportTxt(strBuff.toString(),"phones.txt");
	}

}
